package coding.panel;

import coding.song.Song;
import coding.song.SongList;
import coding.utill.IOSong;

import java.nio.file.LinkPermission;
import java.util.List;
import java.util.Scanner;

/**
 * @创建人 xfliu
 * @创建时间 2021/9/15
 * @描述 主面板
 */
enum Func {
    FUNC1("歌曲清单", 1), FUNC2("喜爱歌单", 2), FUNC3("黑名单", 3), FUNC4("退出系统", 4);
    private String name;
    private int index;

    Func(String name, int index) {
        this.name = name;
        this.index = index;
    }

    public String getName() {
        return name;
    }

    public int getIndex() {
        return index;
    }
}

class MusicThread implements Runnable {
    private Song song;

    public MusicThread(Song song) {
        this.song = song;
    }

    @Override
    public void run() {
        while (true) {
            this.song.setFlag(true);
            this.song.setTreadName(Thread.currentThread().getName());
//            try {
//                Thread.sleep(10000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }

        }


    }
}


public class Panel {
    private final Scanner scanner = new Scanner(System.in);
    private final IOSong ioSong = new IOSong(); //读写歌曲
    private final SongList songList = ioSong.readerSong();

    //添加歌曲面板
    public void addSongPanel() {
        Scanner scanner = new Scanner(System.in);
        int id;
        String songName;
        while (true) {
            System.out.println("===添加歌曲界面====");
            System.out.println("#按0返回上一层");
            System.out.println("请输入id");


            id = scanner.nextInt();
            if (id == 0) {
                return;
            }
            System.out.println("请输入歌曲名字");
            songName = scanner.next();
            Song song = new Song(songName, id);
            boolean flag = songList.addSong(song);
            if (flag) {
                System.out.println(songName + " ：已加入歌单~");
                ioSong.writerSongList(songList);
                return;
            }
            System.out.println("===该歌曲已存在请重新输入===");

        }
    }

    //展示所有的歌曲
    public void displaySong() {
        while (true) {
            songList.querySong();
            System.out.println("#按0返回上一层");
            System.out.println("===正在播放的音乐===");
            List<Song> songs = songList.getSongList();
            for (Song song : songs) {
                if (song.isFlag()) {
                    System.out.println(song.getTreadName()+":"+song.getName() + "正在播放中·····");
                }
            }
            System.out.println("==================");
            System.out.println("请输入需要播放歌曲的名字");
            String songName = scanner.next();
            if (songName.equals("0")) {
                return;
            }
            Song song = songList.serchSong(songName);
            if (song == null) {
                System.out.println("无这歌曲");
            } else {
                musicPlay(song);
            }
        }

    }

    //删除歌曲面板
    public void delSongPanel() {
        while (true) {
            System.out.println("===删除歌曲面板===");
            System.out.println("#按0返回上一层");
            System.out.println("请输入删除歌曲名");
            String songName = scanner.next();
            if (songName.equals("0")) {
                return;
            }
            boolean flag = songList.delSong(songName);
            if (flag) {
                System.out.println(songName + ":删除成功~");
                ioSong.writerSongList(songList);
            } else {
                System.out.println("无此歌曲 请重新操作");
            }
        }
    }


    //修改歌曲
    public void updatePanel() {
        while (true) {
            System.out.println("===修改歌曲界面===");
            System.out.println("#按0返回上一层");
            System.out.println("请输入更改歌曲的名字");
            String oldName = scanner.next();
            if (oldName.equals("0")) {
                return;
            }
            System.out.println("请输入新名字");
            String newName = scanner.next();
            boolean flag = songList.updateSong(newName, oldName);
            ioSong.writerSongList(songList);
        }
    }

    //多线程播放器
    public void musicPlay(Song song) {
        MusicThread musicThread = new MusicThread(song);
        Thread thread = new Thread(musicThread);
        thread.start();
    }


    //歌曲清单面板
    public void songPanel() {
        while (true) {
            System.out.println("=====歌曲清单======");
            System.out.println("1:增加歌曲");
            System.out.println("2:删除歌曲");
            System.out.println("3:修改歌曲");
            System.out.println("4:所有歌曲");
            System.out.println("5:移入黑名单");
            System.out.println("0:返回上一层");
            int input = 0;
            Scanner scanner = new Scanner(System.in);
            try {
                input = scanner.nextInt();
            } catch (Exception e) {
                System.out.println("请输入数字");
                scanner.next();
                continue;
            }
            switch (input) {
                case 1:
                    addSongPanel();
                    break;
                case 2:
                    delSongPanel();
                    break;
                case 3:
                    updatePanel();
                    break;
                case 4:
                    displaySong();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("请输入正确操作");
                    break;
            }
        }
    }

    //主面板
    public void mainPanel() {
        Func[] funcs = Func.values();

        int input = 0;
        while (true) {
            System.out.println("====按数字键操作====");
            for (Func c : funcs) {
                System.out.println(c.ordinal() + ":" + c.getName());
            }
            try {
                input = scanner.nextInt();
                System.out.println(input);
            } catch (Exception e) {
//                e.printStackTrace();
                System.out.println("请输入数字");
                scanner.next();
                continue;
            }
            switch (input) {
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    System.out.println("退出系统~");
                    System.exit(0);
                    break;

                case 0:
                    songPanel();
                    break;
                default:
                    System.out.println("请输入正确的数字");
                    break;
            }
        }
    }

    public static void main(String[] args) {
        Panel panel = new Panel();
        panel.mainPanel();
    }
}
